package com.hengpeng.itfin.sms;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;

import com.hengpeng.itfin.sms.impl.NullSmsPartnerService;
import com.hengpeng.itfin.sms.impl.SmsHsapServiceImpl;
import com.hengpeng.itfinbase.persist.SmsPartner;

/*@Service*/
public class SmsSendFactory
{
	private static Map<Long, SmsPartnerService> gatewayMap = new HashMap<Long, SmsPartnerService>();

	private static final Log logger = LogFactory.getLog(SmsSendFactory.class);

	/**
	 * <pre>
	 * 通过网关配置信息获取处理器
	 * </pre>
	 * @param gateway 网关配置信息
	 * @return 短信网关处理器
	 */
	public static SmsPartnerService getGateway(Long id)
	{
		if (gatewayMap.containsKey(id))
		{
			return gatewayMap.get(id);
		}
		else
		{
			return null;
		}
	}

	/**
	 * <pre>
	 * 通过网关配置信息获取处理器
	 * </pre>
	 * @param context 服务工厂上下文
	 * @param gateway 网关配置信息
	 * @return 短信网关处理器
	 */
	public static SmsPartnerService getGateway(ApplicationContext context, SmsPartner partner)
	{
		if (partner == null)
		{
			return null;
		}
		if (gatewayMap.containsKey(partner.getId()))
		{
			return gatewayMap.get(partner.getId());
		}
		else
		{
			if (partner.getStatus() == SmsPartner.SmsPartnerStatus.OPEN)
			{
				SmsPartnerService initGateway = initGateway(context, partner);
				synchronized (gatewayMap)
				{
					gatewayMap.put(partner.getId(), initGateway);
				}
				return initGateway;
			}
			else
			{
				return null;
			}
		}
	}

	/**
	 * <pre>
	 * 重置短信网关处理器
	 * </pre>
	 * @param gatewayId 网关编号
	 */
	public static void clearGateway(Long partnerId)
	{
		logger.info("重置短信网关处理器{" + partnerId + "}...");
		if (gatewayMap.containsKey(partnerId))
		{
			SmsPartnerService processor = gatewayMap.get(partnerId);
			synchronized (gatewayMap)
			{
				gatewayMap.remove(partnerId);
			}
			try
			{
				processor.release();
			}
			catch (Throwable e)
			{
				logger.info("重置短信网关处理器{" + partnerId + "}释放资源异常...");
			}
		}
	}

	/**
	 * <pre>
	 * 初始化网关处理器
	 * </pre>
	 * @param context 服务工厂上下文
	 * @param gateway 网关配置信息
	 * @return 短信网关处理器
	 */
	private static SmsPartnerService initGateway(ApplicationContext context, SmsPartner partner)
	{
		if (partner == null)
		{
			return new NullSmsPartnerService(context, partner);
		}
		logger.info("初始化短信网关{" + partner.getId() + "}...");
		SmsPartnerService processor = null;
		switch (partner.getSmsSendType())
		{
			case HSAP:
				processor = new SmsHsapServiceImpl(context, partner);
				break;
			default:
				processor = new SmsHsapServiceImpl(context, partner);
				break;
		}
		return processor;
	}

	public static Collection<SmsPartnerService> getAllProcessor()
	{
		synchronized (gatewayMap)
		{
			return new ArrayList<SmsPartnerService>(gatewayMap.values());
		}
	}

}
